package cn.jb698.codec.msm.generator.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * 输入输出流工具类
 * @author LiuJianbin
 *
 */
public class StreamUtils {
	
	
	private static int DEFAULT_BUFFER_SIZE = 4096; // 默认缓冲大小
	
	
	/**
	 * 复制输入流的内容到输出流,默认缓存为2048.
	 * 
	 * @param input
	 *            输入流
	 * @param output
	 *            输出流
	 * @throws IOException
	 *             流操作错误抛异常
	 */
	public static void copy(InputStream input, OutputStream output)
			throws IOException {
		copy(input, output, DEFAULT_BUFFER_SIZE);
	}

	/**
	 * 复制输入流的内容到输出流.
	 * 
	 * @param input
	 *            输入流
	 * @param output
	 *            输出流
	 * @param bufferSize
	 *            缓存大小(字节).
	 * @throws IOException
	 *             流操作错误抛异常
	 */
	public static void copy(InputStream input, OutputStream output, int bufferSize) throws IOException {
		byte[] buf = new byte[bufferSize];
		int bytesRead = input.read(buf);
		while (bytesRead != -1) {
			output.write(buf, 0, bytesRead);
			bytesRead = input.read(buf);
		}
		output.flush();
	}
	

    
	/**
	 * 取环境路径下文件的输入流
	 * @param resourcePath 路径. ( 如 "/java/lang/Object.class" ).
	 * @return 取到的输入流.
	 */
	public static InputStream getResourceAsStream(String resourcePath){

		InputStream rtIs = null;
		
		if(resourcePath != null){
			rtIs = Object.class.getResourceAsStream(resourcePath) ;
		}
		return rtIs;
	}

	 /** 
     * 从 InputStream 读取 固定字符编码的字符串 
     * @param in 
     * @param encoding 编码, 默认为 "ISO-8859-1"
     * @param bufferSize 缓冲区大小 默认 4096 
     * @return 
	 * @throws IOException 
     */  
	public static String readStringFromInputStream(InputStream in,String encoding , Integer bufferSize ) throws IOException {  

		if(bufferSize == null ){
			bufferSize = DEFAULT_BUFFER_SIZE;
		}
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();  
        byte[] data = new byte[bufferSize];  
        int count = -1;  
        while((count = in.read(data,0,bufferSize)) != -1) {
        	outStream.write(data, 0, count);  
        }
        data = null; 
        if(encoding == null){
        	encoding = "ISO-8859-1";
        }
        return new String(outStream.toByteArray(),encoding); 
    }  	

	   
    /**
     * 从 输入流 读出 字节数组
     * @param inputStream
     * @param bufferSize 缓冲大小,默认 4096 
     * @return
     * @throws IOException
     */
    public static byte[] readByteArrayFromInputStream(InputStream inputStream , Integer bufferSize ) throws IOException {
		ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
		if(bufferSize == null ){
			bufferSize = DEFAULT_BUFFER_SIZE;
		}
		byte[] buffer = new byte[bufferSize];
		int bytesRead = inputStream.read(buffer);
		while (bytesRead != -1) {
			byteArrayOutputStream.write(buffer, 0, bytesRead);
			bytesRead = inputStream.read(buffer);
		}
		return byteArrayOutputStream.toByteArray();
      }

    /**
     * 从 输入流 读出 字节数组
     * @param inputStream
     * @return
     * @throws IOException
     */
    public static byte[] readByteArrayFromInputStream(InputStream inputStream ) throws IOException {
    	return readByteArrayFromInputStream(inputStream ,DEFAULT_BUFFER_SIZE );
     }

    
    /**
     * 取一个字节数组的 输入流 
     * @param byteArray 
     * @return 
     */
    public static InputStream getInputStreamFromByteArray(byte[] byteArray){  
        InputStream is = new ByteArrayInputStream(byteArray);  
        return is;  
    } 

 

    
    
    
/** 扩展封装 START */

    
    
    
/** 扩展封装 END */    
    


    
    

/****  外部摘抄来的方法   ** */    
    
    /**
     * 从输入流内读取字节数组
     * @param inputStream
     * @return
     * @throws IOException
     */
    public static byte[] readInputStream(InputStream inputStream) throws IOException {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        byte[] buffer = new byte[16*1024];
        try {
          int bytesRead = inputStream.read(buffer);
          while (bytesRead!=-1) {
            outputStream.write(buffer, 0, bytesRead);
            bytesRead = inputStream.read(buffer);
          }
        } catch (IOException e) {
          throw e;
        }
        return outputStream.toByteArray();
      }
      
    
    /**
     * Closes the given stream. The same as calling {@link InputStream#close()}, but
     * errors while closing are silently ignored.
     */
    public static void closeSilently(InputStream inputStream) {
      try {
        if(inputStream != null) {
          inputStream.close();
        }
      } catch(IOException ignore) {
        // Exception is silently ignored
      }
    }

    /**
     * Closes the given stream. The same as calling {@link OutputStream#close()}, but
     * errors while closing are silently ignored.
     */
    public static void closeSilently(OutputStream outputStream) {
      try {
        if(outputStream != null) {
          outputStream.close();
        }
      } catch(IOException ignore) {
        // Exception is silently ignored
      }
    }
    
    


}
